### Preliminary ---------------------------------------------------------------
# Load libraries
library(cowplot)
library(modelsummary)
library(scales)
library(tidyverse)
library(usmap)

# Load data
legislator_df <- read_csv("Legislator Race Data.csv")

us_df <- read_csv("Asian Population and State Legislative Representation (1970 - 2020).csv")
load("District Geodata.Rda")

geo_df <- filter(geo_df, !(is.na(AsianRep)))

# Specify options
options("modelsummary_format_numeric_latex" = "plain")

### Table S2: Descriptive Statistics on AA State Legislators ------------------
# Group data by legislator ID
graphing_data <- legislator_df %>%
  filter(Asian == 1) %>%
  group_by(KlarnerID) %>%
  summarize(Party = last(Party),
            Nationality = last(Nationality),
            Race = last(Race),
            StateAbb = last(StateAbb)) %>%
  mutate(Nationality = case_when(
    str_detect(Nationality, "philipp") ~ "Filipino",
    str_detect(Nationality, "india") ~ "Indian",
    str_detect(Nationality, "hawaii") ~ "Native Hawaiian",
    str_detect(Nationality, "japan") ~ "Japanese",
    str_detect(Nationality, "vietn") ~ "Vietnamese",
    str_detect(Nationality, "korea") ~ "Korean",
    str_detect(Nationality, "chin") ~ "Chinese or Taiwanese",
    str_detect(Nationality, "hong") ~ "Chinese or Taiwanese",
    str_detect(Nationality, "taiw") ~ "Chinese or Taiwanese",
    is.na(Nationality) ~ "Unknown",
    TRUE ~ "Other Asian"
  ),
  Race = case_when(
    str_detect(Race, "s") ~ "South Asian",
    str_detect(Race, "e") ~ "East Asian",
    str_detect(Race, "p") ~ "Pacific Islander"
  ),
  StateAbb = case_when(
    StateAbb %in% c("AK", "IN", "KS", "KY", "MO", "NC", "NE", "NM", "OH",
               "OR", "TN", "VT", "WI", "WV", "WY") ~ "Other",
    TRUE ~ StateAbb),
  Party = case_when(
    Party == "d" ~ "Democratic",
    Party == "r" ~ "Republican",
    TRUE ~ "Other")
  )

# Create summary table
datasummary(Race + Nationality + Party + StateAbb ~ N + Percent(),
            fmt = 1,
            output = "latex",
            data = graphing_data)


### Figure 1: Graph on Asian Representation Across Time and District -----------
# Create graph of over time representation/population growth
time_plot <- ggplot(us_df, aes(x = Year, lty = Variable,
                          group = Variable, shape = Variable)) +
  geom_point(aes(y = Percent)) +
  geom_line(aes(y = PercentInterp)) +
  scale_y_continuous(labels = percent_format(scale = 1)) +
  scale_color_manual(values = c("Salmon4", "SteelBlue4")) + 
  labs(title = "Growth in Asian Population and Representation", x = NULL, 
       y = NULL, color = NULL, shape = NULL, lty = NULL) +
  theme_bw() +
  theme(
    legend.position = c(0.3, 0.8),
    legend.background = element_rect(color = "black"),
    aspect.ratio = 0.58
  )


# Create map showing the district location of AA state representatives ---------
map_plot <- ggplot(geo_df) +
  geom_sf(fill = "white", color = "grey60") +
  geom_point(data = filter(geo_df, 
                           AsianRep == 1),
             aes(X, Y), 
             color = "black",
             size = 1.5,
             alpha = 0.8) + 
  coord_sf(xlim = c(-125, -68),
           ylim = c(23, 50)) +
  labs(x = NULL,
       y = NULL,
       title = "Map of Asian American Representation (2010s)") + 
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

# Combine plots in vertical arrangement
plot_grid(time_plot,
          map_plot,
          nrow = 2)

